// 提供复用逻辑的函数  懒加载
import { ref,onUnmounted } from 'vue'
import { useIntersectionObserver } from '@vueuse/core'
import dayjs from "dayjs";
import { useIntervalFn } from "@vueuse/core";
export const useLazyData = (target, apiFn) => {
  const result = ref([])
  const { stop } = useIntersectionObserver(
    // 监听的目标元素
    target,
    ([{ isIntersecting }], observerElement) => {
      // 是否进入可视区
      if (isIntersecting) {
        // stop 是停止观察是否进入或移出可视区域的行为
        stop()
        // 调用api接口
        apiFn().then(data => {
          result.value = data.result
        })
      }
    }, {
      // 配置选项，相交的比例大于0就触发
      threshold: 0
    }
  )
  return result
}
export const payCountDown=()=>{
  const time = ref(0);
  const timeText = ref("");
  // console.log(time.value);
  const { pause, resume } = useIntervalFn(() => {
    time.value--;
    timeText.value = dayjs.unix(time.value).format("mm分ss秒");
    if (time.value <= -1) {
      //停止定时器
      pause();
    }
  }, 1000);
  onUnmounted(() => {
    pause();
  });
  const start=(countdown)=>{
    time.value=countdown
    resume()
    timeText.value = dayjs.unix(time.value).format("mm分ss秒");
  }
  return {
    start,
    timeText
  }
}

